{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6666666666666667"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def holding_period_return(price_end, price_beginning, dividends_earned):\n",
    "    '''\n",
    "    SUMMARY: Calculate the holding period return for an asset\n",
    "    \n",
    "    PARA price_end: The price at the end of the period.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA price_beginning: The price at the beginning of the period.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA dividends_earned: The dividends earned during the period\n",
    "    TYPE float:\n",
    "    \n",
    "    '''  \n",
    "    \n",
    "    return (price_end + dividends_earned) / price_beginning - 1\n",
    "\n",
    "#EXAMPLE\n",
    "pb = 3.00\n",
    "pe = 4.00\n",
    "di = 1.00\n",
    "holding_period_return(pe, pb, di)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3152060571428128"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def geometric_mean(returns):\n",
    "    '''\n",
    "    SUMMARY: Calculate the geometric mean of a given array of returns.\n",
    "    \n",
    "    PARA returns: A list of returns\n",
    "    TYPE list:\n",
    "    \n",
    "    '''\n",
    "    \n",
    "    import numpy as np\n",
    "    \n",
    "    return_array = np.array(returns)\n",
    "    \n",
    "    return return_array.prod()**(1.0/len(return_array)) - 1\n",
    "\n",
    "#EXAMPLE\n",
    "r = [1.25, 1.30, 1.40]\n",
    "geometric_mean(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.16399999999999998"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def expected_returns(market_return, beta, risk_free_rate):\n",
    "    '''\n",
    "    SUMMARY: Calculate the expected return for an asset.\n",
    "    \n",
    "    PARA market_return: The expected return for the market.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA beta: The assets beta.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA risk_free_rate: The rate of return for  a risk free asset, usually t-bill.\n",
    "    TYPE float:\n",
    "    \n",
    "    '''      \n",
    "    \n",
    "    return risk_free_rate + (beta * (market_return - risk_free_rate))\n",
    "\n",
    "\n",
    "# EXAMPLE\n",
    "rfr = .08\n",
    "mrt = .15\n",
    "bet = 1.2\n",
    "expected_returns(mrt, bet, rfr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.7499999999999998"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def capital_market_line(return_market, risk_free_rate, std_market):\n",
    "    '''\n",
    "    SUMMARY: Calculate the capital market line for a portfolio\n",
    "    \n",
    "    PARA market_return: The expected return for the market.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA std_market: The standard deviation for market returns.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA risk_free_rate: The rate of return for  a risk free asset, usually t-bill.\n",
    "    TYPE float:\n",
    "    \n",
    "    ''' \n",
    "    \n",
    "    return (return_market - risk_free_rate) / std_market\n",
    "\n",
    "#EXAMPLE\n",
    "rfr = .08\n",
    "mkr = .15\n",
    "mks = .04\n",
    "capital_market_line(mkr, rfr, mks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.15000000000000002"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def m_squared(return_portfolio, risk_free_rate, return_market, std_market, std_portfolio):\n",
    "    '''\n",
    "    SUMMARY: Calculate the m-squared for a portfolio\n",
    "    \n",
    "    PARA return_portfolio: The expected return for the portfolio.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA std_portfolio: The standard deviation for portfolio returns.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA market_return: The expected return for the market.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA market_std: The standard deviation for market returns.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA risk_free_rate: The rate of return for  a risk free asset, usually t-bill.\n",
    "    TYPE float:\n",
    "    \n",
    "    ''' \n",
    "        \n",
    "    return ((return_portfolio - risk_free_rate) * (std_market / std_portfolio)) + (return_market - risk_free_rate)\n",
    "\n",
    "\n",
    "#EXAMPLE\n",
    "rfr = .08\n",
    "mkr = .15\n",
    "mks = .04\n",
    "ptr = .20\n",
    "pts = .06\n",
    "m_squared(ptr, rfr, mkr, mks, pts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.7499999999999998"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def treynor_measure(return_portfolio, risk_free_rate, beta_portfolio):\n",
    "    '''\n",
    "    SUMMARY: Calculate the treynor measure for a portfolio\n",
    "    \n",
    "    PARA return_portfolio: The expected return for the portfolio.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA risk_free_rate: The rate of return for  a risk free asset, usually t-bill.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA beta_portfolio: The portfolio beta.\n",
    "    TYPE float:\n",
    "    \n",
    "    ''' \n",
    "    \n",
    "    return (return_portfolio - risk_free_rate) / beta_portfolio\n",
    "\n",
    "#EXAMPLE\n",
    "rfr = .08\n",
    "ptr = .15\n",
    "btp = .04\n",
    "treynor_measure(ptr, rfr, btp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.06839999999999999"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def jensen_alpha(return_portfolio, risk_free_rate, beta_portfolio, return_market):\n",
    "    '''\n",
    "    SUMMARY: Calculate the jensen alpha measure for a portfolio\n",
    "    \n",
    "    PARA return_portfolio: The expected return for the portfolio.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA risk_free_rate: The rate of return for  a risk free asset, usually t-bill.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA beta_portfolio: The portfolio beta.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA market_return: The expected return for the market.\n",
    "    TYPE float:\n",
    "    \n",
    "    ''' \n",
    "    \n",
    "    return return_portfolio - (risk_free_rate + beta_portfolio * (return_market - risk_free_rate))\n",
    "\n",
    "#EXAMPLE\n",
    "ptr = .15\n",
    "rfr = .08\n",
    "ptb = .04\n",
    "mkr = .12\n",
    "jensen_alpha(ptr, rfr, ptb, mkr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def price_weighted_index(list_of_stocks):\n",
    "    '''\n",
    "    SUMMARY: Given a list of stock prices, calculate the price weighted index for a given period.\n",
    "    \n",
    "    PARA list_of_stocks: A list of stock prices.\n",
    "    TYPE list:\n",
    "    \n",
    "    ''' \n",
    "    \n",
    "    number_of_stocks = len(list_of_stocks)\n",
    "    sum_of_stocks = sum(list_of_stocks)\n",
    "    \n",
    "    return sum_of_stocks / number_of_stocks\n",
    "\n",
    "# EXAMPLE\n",
    "ls = [10, 20 ,60]\n",
    "price_weighted_index(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "125.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def market_cap_weight(shares_outstanding, base_price, future_price):\n",
    "    '''\n",
    "    SUMMARY: Calculate the value of an index using the market cap weight method.\n",
    "    \n",
    "    PARA shares_outstanding: A list of the shares outstanding for each stock.\n",
    "    TYPE list:\n",
    "    \n",
    "    PARA base_price: A list of the shares base price for the base period.\n",
    "    TYPE list:\n",
    "    \n",
    "    PARA future_price: A list of the shares future price for the future period.\n",
    "    TYPE list:\n",
    "    \n",
    "    '''     \n",
    "    \n",
    "    import numpy as np\n",
    "    \n",
    "    #convert everything to numpy arrays.\n",
    "    shares_outstanding = np.array(shares_outstanding)\n",
    "    base_price = np.array(base_price)\n",
    "    future_price = np.array(future_price)\n",
    "    \n",
    "    # calculate the base\n",
    "    base = shares_outstanding * base_price\n",
    "    \n",
    "    # calculate the numerator\n",
    "    numerator = shares_outstanding * future_price\n",
    "    \n",
    "    return sum(numerator) / sum(base) * 100\n",
    "\n",
    "#EXAMPLE\n",
    "base_price = [100, 10, 1]\n",
    "shares_outstanding = [100000, 1000000, 20000000]\n",
    "future_price = [200, 10, 1]\n",
    "market_cap_weight(shares_outstanding, base_price, future_price)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "392.15686274509807"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def return_on_equity(net_income, book_value):\n",
    "    '''\n",
    "    SUMMARY: Calculate the return on equity\n",
    "    \n",
    "    PARA net_income: The net income for the period..\n",
    "    TYPE float:\n",
    "\n",
    "    PARA book_value: Book value per share.\n",
    "    TYPE float:\n",
    "    \n",
    "    '''\n",
    "    \n",
    "    return net_income / book_value\n",
    "\n",
    "#EXAMPLE\n",
    "ni = 10000\n",
    "bv = 25.50\n",
    "return_on_equity(ni, bv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "456.3869462012922"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def bond_value(maturity_value, interest_rate, num_of_years):\n",
    "    '''\n",
    "    SUMMARY: Calculate the value of a bond, given the maturity value, number of years to maturity and interest rate.\n",
    "    \n",
    "    PARA maturity_value: The value of the bond at maturity.\n",
    "    TYPE float:\n",
    "\n",
    "    PARA interest_rate: The semi-annual discount rate on the bond.\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA num_of_years: The life of the bond in N years.\n",
    "    TYPE float:\n",
    "    \n",
    "    '''\n",
    "    \n",
    "    return maturity_value / (1 + interest_rate) ** (num_of_years * 2)\n",
    "\n",
    "\n",
    "#EXAMPLE\n",
    "i = 0.04\n",
    "n = 10\n",
    "v = 1000\n",
    "bond_value(v, i, n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.07474772642332128"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def current_yield(annual_coup_pay, bond_price):\n",
    "    '''\n",
    "    SUMMARY: Calculate the current yield of a bond.\n",
    "    \n",
    "    PARA annual_coup_pay: The coupon payment made annually.\n",
    "    TYPE float:\n",
    "\n",
    "    PARA bond_price: the bonds current price.\n",
    "    TYPE int:\n",
    "    \n",
    "    '''\n",
    "    \n",
    "    return annual_coup_pay / bond_price\n",
    "\n",
    "#EXAMPLE\n",
    "a = 60\n",
    "b = 802.70\n",
    "current_yield(a, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "802.0722611657345"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def yield_to_maturity(semi_coup, years, yield_to_maturity, par_value):\n",
    "    '''\n",
    "    SUMMARY: calculate present value of a bond using the yield to maturity method.\n",
    "    \n",
    "    PARA semi_coup: The coupon payment made semi-annually.\n",
    "    TYPE float:\n",
    "\n",
    "    PARA years: The number of years till bond maturity.\n",
    "    TYPE int:\n",
    "    \n",
    "    PARA yield_to_maturity: The bond's yield to maturity. This will be divided by 2 in the calculate\n",
    "    TYPE float:\n",
    "    \n",
    "    PARA par_value: The bonds par value.\n",
    "    TYPE float:    \n",
    "    \n",
    "    '''\n",
    "    \n",
    "    # calculate number of iterations\n",
    "    n = years * 2\n",
    "    \n",
    "    # initalize a total to hold the final present value total\n",
    "    present_value = 0\n",
    "    \n",
    "    for i in range(1, n + 1):\n",
    "                \n",
    "        if i != n:\n",
    "            # if it doesn't equal the last period calculate the present value of the coupan payment\n",
    "            disc_coup = semi_coup / ((1 + (yield_to_maturity / 2))** i)\n",
    "            present_value += disc_coup\n",
    "        else:    \n",
    "            # if it is the last period calculate the present value of the last coupon payment plus the principle\n",
    "            terminal = (semi_coup + par_value) / ((1 + (yield_to_maturity / 2))** i)\n",
    "            present_value += terminal\n",
    "            \n",
    "    return present_value\n",
    "\n",
    "#EXAMPLE\n",
    "c = 30\n",
    "n = 20\n",
    "y = .08\n",
    "p = 1000\n",
    "yield_to_maturity(c, n, y, p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1215384615384616"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def forward_rate(spot_rate, periods):\n",
    "    '''\n",
    "    SUMMARY: Calculate the forward rate given a spot rate.\n",
    "    \n",
    "    PARA spot_rate: The spot rate for each period.\n",
    "    TYPE float:\n",
    "\n",
    "    PARA periods: The number of periods to be calculated.\n",
    "    TYPE int: \n",
    "    \n",
    "    '''    \n",
    "    spot_n = (1 + (spot_rate * periods)) ** periods\n",
    "    spot_n_1 = (1 + (spot_rate * (periods - 1))) ** (periods - 1)\n",
    "    \n",
    "    return (spot_n / spot_n_1) - 1\n",
    "\n",
    "#EXAMPLE\n",
    "s = .04\n",
    "n = 2\n",
    "forward_rate(s, n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000.9762250328288"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "def bond_value_forward(coup, par_value, forward_rates):\n",
    "    '''\n",
    "    SUMMARY: Calculate a bond's value given forward rates.\n",
    "    \n",
    "    PARA coup: The semi-annual coupon payment.\n",
    "    TYPE float:\n",
    "\n",
    "    PARA par_value: The bond's par value\n",
    "    TYPE float: \n",
    "    \n",
    "    PARA forward_rates: The forward rate for each period to be calculated.\n",
    "    TYPE list: \n",
    "    \n",
    "    '''    \n",
    "    \n",
    "    forward_rates = np.array([(rate + 1) for rate in forward_rates])\n",
    "    total = 0\n",
    "    \n",
    "    for index, item in enumerate(forward_rates):\n",
    "        \n",
    "        base = np.product(forward_rates[:index + 1])\n",
    "        \n",
    "        if item != forward_rates[-1]:            \n",
    "            # if it doesn't equal the last period then we calulate the discounted coupon payment        \n",
    "            disc_value = coup / base\n",
    "            total += disc_value\n",
    "\n",
    "        else:\n",
    "            # if it does equal the last period then we calulate the discounted coupon payment + par value\n",
    "            disc_value = (coup + par_value)/ base\n",
    "            total += disc_value\n",
    "\n",
    "    return total\n",
    "\n",
    "# EXAMPLE\n",
    "c = 50\n",
    "p = 1000\n",
    "r = [.04, .05, .06]\n",
    "bond_value_forward(c, p, r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
